package com.binyong.config;

import com.binyong.constant.RedisConstant;
import com.binyong.redis.service.impl.TestSubscribeServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * redis消息监听
 * @author binyong
 *
 */
@Configuration
public class RedisListenerConfig {
 
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,TestSubscribeServiceImpl testSubscribeServiceImpl) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(connectionFactory);
        //增加消息监听方法，可以使用固定字符匹配或者使用通配符匹配
        //固定字符匹配
        redisMessageListenerContainer.addMessageListener(testSubscribeServiceImpl, new ChannelTopic(RedisConstant.msg_queue));
        //通配符匹配
        //redisMessageListenerContainer.addMessageListener(testSubscribeServiceImpl, new PatternTopic(RedisConstant.msg_queue));

        return redisMessageListenerContainer;
    }
}
